package controllers

import (
	"encoding/json"
	"fmt"
	"merge_Web/gamedata"
	"merge_Web/models"
	"time"

	httplib "github.com/beego/beego/v2/client/httplib"
	beego "github.com/beego/beego/v2/server/web"
)

type CommonResult struct {
	Code int `json:"code"`
}

type SendEmailResult struct {
	Code int `json:"code"`
}

type User struct {
	Name     string `json:"Name"`
	Password string `json:"password"`
}

type UserInfo struct {
	Id   string `json:"Id"`
	Name string `json:"Name"`
	Code int    `json:"code"`
}

type UserIpId struct {
	Ip    string `json:"Ip"`
	DwUin string `json:"DwUin"`
}

type RewardItem struct {
	Id  string `json:"Id"`
	Cnt string `json:"Cnt"`
}

type SendEmailInfo struct {
	Ip        string       `json:"Ip"`
	IsAll     bool         `json:"IsAll"`
	UserIds   []string     `json:"UserIds"`
	Diamond   string       `json:"Diamond"`
	Energy    string       `json:"Energy"`
	LinkURL   string       `json:"LinkURL"`
	ItemInfos []RewardItem `json:"ItemInfos"`
	Title     string       `json:"Title"`
	Content   string       `json:"Content"`
}

type LoginController struct {
	beego.Controller
}

func (c *LoginController) Get() {
	// c.Data["Website"] = "beego.vip"
	// c.Data["Email"] = "astaxie@gmail.com"
	c.TplName = "index.html"
}

func (c *LoginController) Login() {
	u := &User{}
	json.Unmarshal(c.Ctx.Input.RequestBody, u)
	orm := models.GetNewOrm()
	var account models.Account = models.Account{UserName: u.Name}
	err1 := orm.Raw("SELECT * FROM t_account WHERE user_name = ? and user_password = ?", u.Name, u.Password).QueryRow(&account)
	if err1 != nil {
		fmt.Println(err1)
		res := CommonResult{Code: 2}
		c.Data["json"] = &res
		c.ServeJSON()
	} else {
		res := CommonResult{Code: 200}
		c.Data["json"] = &res
		c.ServeJSON()
	}
}
func (c *LoginController) LoginConfirm() {

	var svrinfos []models.Server_info
	orm := models.GetNewOrm()
	orm.Raw("SELECT * FROM t_Server_info").QueryRows(&svrinfos)
	ipArr := []string{"127.0.0.1"}

	for i := 0; i < len(svrinfos); i++ {
		ipArr = append(ipArr, svrinfos[i].SvrIpAddr)
	}

	c.Data["SvrIpList"] = ipArr
	c.TplName = "email.html"
}

func (c *LoginController) RenewEmail() {

	var svrinfos []models.Server_info
	orm := models.GetNewOrm()
	orm.Raw("SELECT * FROM t_Server_info").QueryRows(&svrinfos)
	ipArr := []string{"127.0.0.1"}

	for i := 0; i < len(svrinfos); i++ {
		ipArr = append(ipArr, svrinfos[i].SvrIpAddr)
	}

	c.Data["SvrIpList"] = ipArr
	c.TplName = "email.html"
}

func (c *LoginController) RenewAnnounce() {
	ipArr := []string{"Android", "IOS"}
	c.Data["PlatformName"] = ipArr
	c.TplName = "Announce.html"
}

func (c *LoginController) SaveVersion() {
	u := &models.ServerVersionData{}
	json.Unmarshal(c.Ctx.Input.RequestBody, u)
	orm := models.GetNewOrm()

	_, err1 := orm.InsertOrUpdate(u)
	if err1 != nil {
		fmt.Println(err1)
		res := CommonResult{Code: 2}
		c.Data["json"] = &res
		c.ServeJSON()
	} else {
		res := CommonResult{Code: 200}
		c.Data["json"] = &res
		c.ServeJSON()
	}
}

func (c *LoginController) Register() {

	u := &User{}
	json.Unmarshal(c.Ctx.Input.RequestBody, u)
	orm := models.GetNewOrm()
	var account models.Account = models.Account{UserName: u.Name}
	err1 := orm.Raw("SELECT * FROM t_account WHERE user_name = ?", u.Name).QueryRow(&account)

	if err1 != nil {
		account.UserName = u.Name
		account.UserPassword = u.Password

		id, err := orm.Insert(&account)
		if err == nil {
			fmt.Println(id)
			res := CommonResult{Code: 200}
			c.Data["json"] = &res
		}
	} else {
		res := CommonResult{Code: 1}
		c.Data["json"] = &res
	}
	c.ServeJSON()
}

func (c *LoginController) LoadConfig() {
	var data = gamedata.MergeCfgData
	c.Data["json"] = &data
	c.ServeJSON()
}

func (c *LoginController) AddEmailUser() {
	u := &UserIpId{}
	json.Unmarshal(c.Ctx.Input.RequestBody, u)
	// orm := models.GetNewOrm()

	var account UserInfo = UserInfo{}

	account.Id = u.DwUin
	account.Name = "random"

	account.Code = 200
	c.Data["json"] = &account

	c.ServeJSON()
}

func (c *LoginController) SendEmail() {
	req := &SendEmailInfo{}
	json.Unmarshal(c.Ctx.Input.RequestBody, req)

	fmt.Println(req)
	req1 := httplib.Post("http://" + req.Ip + ":8081" + "/api/SendEmail")
	req1.SetTimeout(100*time.Second, 30*time.Second)
	req1.JSONBody(req)

	res := &SendEmailResult{}
	req1.ToJSON(res)
	c.Data["json"] = res
	c.ServeJSON()
}
